package com.cctc.rds.nrpc.core.protocol.serializer.impl;

import com.cctc.rds.nrpc.core.protocol.serializer.Serializer;
import com.cctc.rds.nrpc.core.protocol.serializer.SerializerType;
import com.cctc.rds.nrpc.core.util.JsonUtils;

import java.nio.charset.StandardCharsets;

public class JsonSerializer implements Serializer {
    @Override
    public Byte getSerializerType() {
        return SerializerType.JSON.getValue();
    }

    @Override
    public byte[] serialize(Object o) {
        return JsonUtils.toJson(o).getBytes(StandardCharsets.UTF_8);
    }

    @Override
    public <T> T deserialize(Class<T> tClass, byte[] data) {
        return JsonUtils.fromJson(new String(data, StandardCharsets.UTF_8), tClass);
    }
}
